package com.cheguo.zeus.biz.join.facade.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cheguo.zeus.biz.join.entity.JoinCarDealerContractView;
import com.cheguo.zeus.biz.join.service.JoinCarDealerContractViewService;
import com.cheguo.zeus.join.facade.JoinCarDealerService;
import com.cheguo.zeus.join.request.JoinQueryDealerInfoDto;
import com.cheguo.zeus.join.response.JoinCarDealerContractViewResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class JoinCarDealerServiceImpl implements JoinCarDealerService {


    @Autowired
    private JoinCarDealerContractViewService joinCarDealerContractViewService;


    private static Logger LOG = LoggerFactory.getLogger(JoinCarDealerServiceImpl.class);


    @Override
    public List<JoinCarDealerContractViewResponse> getDealerAndContractInfo(JoinQueryDealerInfoDto joinQueryDealerInfoDto) {

        try {
            if (null == joinQueryDealerInfoDto) {
                LOG.error("获取车商与合同信息的查询参数为空");
                return null;
            }
            //参数组装
            JoinCarDealerContractView joinCarDealerContractView = assembleQueryDealerAndDealerInfoParam(joinQueryDealerInfoDto);

            return joinCarDealerContractViewService.getDealerAndContractInfo(joinCarDealerContractView);

        } catch (Exception e) {
            LOG.error("获取车商与合同信息异常,异常信息为:",e);
            return null;
        }
    }


    @Override
    public Page<JoinCarDealerContractViewResponse> getDealerAndContractInfoByPage(JoinQueryDealerInfoDto joinQueryDealerInfoDto, Integer currentPage, Integer pageSize) {

        try {
            LOG.info("获取车商与合同信息的查询信息为:{}", JSON.toJSONString(joinQueryDealerInfoDto));

            //组装参数
            JoinCarDealerContractView joinCarDealerContractView = assembleQueryDealerAndDealerInfoParam(joinQueryDealerInfoDto);
            LOG.info("向数据库查询获取车商与合同信息的参数为:{}", JSON.toJSONString(joinCarDealerContractView));

            //组装分页sql查询条件
            Page<JoinCarDealerContractView> page = new Page<>(currentPage,pageSize);
            EntityWrapper<JoinCarDealerContractView> entityWrapper = new EntityWrapper<>();
            entityWrapper.gt("join_status", 0);

            if (StringUtils.isNotBlank(joinCarDealerContractView.getDealerName())) {
                entityWrapper.like("dealer_name", joinCarDealerContractView.getDealerName());
            }

            if (StringUtils.isNotBlank(joinCarDealerContractView.getProvinceName())) {
                entityWrapper.eq("province_name", joinCarDealerContractView.getProvinceName());
            }

            if (StringUtils.isNotBlank(joinCarDealerContractView.getCityName())) {
                entityWrapper.eq("city_name", joinCarDealerContractView.getCityName());
            }

            if (StringUtils.isNotBlank(joinCarDealerContractView.getOrgName())) {
                entityWrapper.eq("org_name", joinCarDealerContractView.getOrgName());
            }

            if (null != joinCarDealerContractView.getJoinType()) {
                entityWrapper.eq("join_type", joinCarDealerContractView.getJoinType());
            }

            if (null != joinCarDealerContractView.getBoardStatus()) {
                entityWrapper.eq("board_status", joinCarDealerContractView.getBoardStatus());
            }

            //分页查询
            Page<JoinCarDealerContractView> dealerAndContractInfo = joinCarDealerContractViewService.selectPage(page, entityWrapper);

            //TODO 结果类型转换

            List<JoinCarDealerContractView> recordsList = dealerAndContractInfo.getRecords();
            List<JoinCarDealerContractViewResponse> resultList = new ArrayList<>();

            for (JoinCarDealerContractView joinCarDealerContractView1 : recordsList) {
                JoinCarDealerContractViewResponse joinCarDealerContractViewResponse = new JoinCarDealerContractViewResponse();
                BeanUtils.copyProperties(joinCarDealerContractView1, joinCarDealerContractViewResponse);
                resultList.add(joinCarDealerContractViewResponse);
            }

            //结果组装
            Page<JoinCarDealerContractViewResponse> result = new Page<>();
            result.setSize(dealerAndContractInfo.getSize());
            result.setCurrent(dealerAndContractInfo.getCurrent());
            result.setRecords(resultList);
            result.setTotal(dealerAndContractInfo.getTotal());

            return result;

        } catch (Exception e) {
            LOG.error("获取车商与合同信息异常,异常信息为:{}",e);
            return null;
        }
    }

    /**
     * @Description:参数entity参数
     * @Param joinQueryDealerInfoDto 查询dto
     * @Return 查询参数entity
     * @Author  Dong_hy
     * @Date 2018/5/28 16:55
     */
    private JoinCarDealerContractView assembleQueryDealerAndDealerInfoParam(JoinQueryDealerInfoDto joinQueryDealerInfoDto){
        JoinCarDealerContractView joinCarDealerContractView = new JoinCarDealerContractView();

        joinCarDealerContractView.setCityName(joinQueryDealerInfoDto.getCityName());
        joinCarDealerContractView.setProvinceName(joinQueryDealerInfoDto.getProvinceName());
        joinCarDealerContractView.setDealerName(joinQueryDealerInfoDto.getDealerName());
        joinCarDealerContractView.setOrgName(joinQueryDealerInfoDto.getBranchCompanyName());
        joinCarDealerContractView.setJoinType(joinQueryDealerInfoDto.getJoinType());
        joinCarDealerContractView.setBoardStatus(joinQueryDealerInfoDto.getBoardStatus());


        return joinCarDealerContractView;
    }
}
